document.addEventListener('DOMContentLoaded', function() {
    const clearBtn = document.getElementById('clearStorageBtn');
    const confirmCheckbox = document.getElementById('confirmCheckbox');
    const statusMessage = document.getElementById('statusMessage');
    
    // 检查元素是否存在
    if (!clearBtn || !confirmCheckbox || !statusMessage) {
        console.error('Required elements not found');
        return;
    }
    
    // 当确认复选框状态改变时启用/禁用按钮
    confirmCheckbox.addEventListener('change', function() {
        clearBtn.disabled = !this.checked;
    });
    
    // 清空存储按钮点击事件
    clearBtn.addEventListener('click', function() {
        if (confirmCheckbox.checked) {
            chrome.storage.local.clear(function() {
                if (chrome.runtime.lastError) {
                    showStatus('清空存储失败: ' + chrome.runtime.lastError.message, 'error');
                } else {
                    showStatus('本地存储数据已成功清空！', 'success');
                    // 清空后禁用按钮和复选框
                    confirmCheckbox.checked = false;
                    clearBtn.disabled = true;
                }
            });
        }
    });
    
    // 显示状态消息
    function showStatus(message, type) {
        statusMessage.textContent = message;
        statusMessage.className = 'status ' + type;
        
        // 3秒后自动隐藏消息
        setTimeout(() => {
            statusMessage.removeAttribute('class');
            statusMessage.textContent = '';
        }, 3000);
    }
});